﻿using Aspose.Words;
using ERPSystem.Common.Domains.Delivergoods;
using ERPSystem.Common.Domains.SalesOrderCommom;
using ERPSystem.Common.DTO;
using ERPSystem.Common.DTO.ShippingnoteDTO;
using ERPSystem.Common.Infrastructure.IRepository;
using ERPSystem.Enum.CommonErrorCodes;
using MediatR;

namespace SalesManagementCommon.API.SalesManagementCommons.Orderdetails
{
    public class DeliverydetailsHandler : IRequestHandler<DeliverydetailsCommand, Result<List<DeliverydetailsDTO>>>
    {

        private readonly IBaseRepository<Shippingnote> shippingnoteRepository;
        private readonly IBaseRepository<Materialdelivery> materialdeliveryRepository;
        private readonly IBaseRepository<SaleOutOfWarehouse> saleOutOfWarehouseRepository;
        private readonly IBaseRepository<SellOutMaterials> sellOutMaterialsRepository;
        private readonly IBaseRepository<SalesOrder> salesOrderRepository;

        public DeliverydetailsHandler(IBaseRepository<Shippingnote> shippingnoteRepository, IBaseRepository<Materialdelivery> materialdeliveryRepository, IBaseRepository<SaleOutOfWarehouse> saleOutOfWarehouseRepository, IBaseRepository<SellOutMaterials> sellOutMaterialsRepository, IBaseRepository<SalesOrder> salesOrderRepository)
        {
            this.shippingnoteRepository = shippingnoteRepository;
            this.materialdeliveryRepository = materialdeliveryRepository;
            this.saleOutOfWarehouseRepository = saleOutOfWarehouseRepository;
            this.sellOutMaterialsRepository = sellOutMaterialsRepository;
            this.salesOrderRepository = salesOrderRepository;
        }

        public async Task<Result<List<DeliverydetailsDTO>>> Handle(DeliverydetailsCommand request, CancellationToken cancellationToken)
        {
            var result = new Result<List<DeliverydetailsDTO>>() { Code = (int)CommonErrorCode.Search };
            try
            {
                var shippings = await (await shippingnoteRepository.GetAll()).ToListAsync();
                var materialdelivery = await (await materialdeliveryRepository.GetAll()).ToListAsync();
                var saleOutOfWarehouse = await (await saleOutOfWarehouseRepository.GetAll()).ToListAsync();
                var sellOutMaterials = await (await sellOutMaterialsRepository.GetAll()).ToListAsync();
                var salesOrder = await (await salesOrderRepository.GetAll()).ToListAsync();

                var list = (from a in shippings
                            join b in materialdelivery on a.Serialnumber equals b.Serialnumber
                            join e in salesOrder on a.SalesOrderId equals e.SalesOrderId
                            join c in saleOutOfWarehouse on b.ItemBianma equals c.ItemBianma
                            join d in sellOutMaterials on c.SerialNumber equals d.SerialNumber
                            select new DeliverydetailsDTO()
                            {
                                ShippingnoteId = a.ShippingnoteId,
                                DocumentNumber = a.DocumentNumber,
                                Customername = a.Customername,
                                SalesStaff = e.SalesStaff,
                                DocumentDate = e.DocumentDate,
                                Deliverydate = a.Deliverydate,
                                SalesType = e.SalesType,
                                ItemBianma = b.ItemBianma,
                                MaterialName = d.MaterialName,
                                Numberofpackages = a.Numberpack,
                                OutboundQuantity = d.OutboundQuantity
                            });
                if(!string.IsNullOrEmpty(request.DocumentNumber))
                {
                    list = list.Where(d => d.DocumentNumber == request.DocumentNumber).ToList();
                }
                if (!string.IsNullOrEmpty(request.Customername))
                {
                    list = list.Where(d => d.Customername == request.Customername).ToList();
                }
                if (!string.IsNullOrEmpty(request.SalesStaff))
                {
                    list = list.Where(d => d.SalesStaff == request.SalesStaff).ToList();
                }
                if (!string.IsNullOrEmpty(request.SalesType))
                {
                    list = list.Where(d => d.SalesType == request.SalesType).ToList();
                }
                if (!string.IsNullOrEmpty(request.MaterialName))
                {
                    list = list.Where(d => d.MaterialName == request.MaterialName).ToList();
                }


                result.Data = list.ToList();


            }
            catch (Exception)
            {

                throw;
            } 
           
            return result;


        }
    }
}
